package net.wantongtech.xa.uploadbasicinfo.dao;

import com.wtkj.dao.impl.BaseDataTemplate;
import com.wtkj.dao.impl.GenericResultSetExtractor;
import com.wtkj.vo.dzzf.base.clearing.TransOutExtend;
import com.wtkj.vo.dzzf.base.jygl.TransOutDspt;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.sql.ResultSet;
import java.util.List;

/**
 * 填充历史争议交易数据服务实现
 *
 * @author MaShibo
 * 日期 :  2020/2/10
 */
@Repository
public class FillHistoryDisputedDataDao extends BaseDataTemplate {


    public boolean existsNewDisputedId(Long disputedId) {
        final String sql = "SELECT 1 from tr_dispute_transout tr WHERE tr.disputedid=?";
        return jdbcTemplate.query(sql, ResultSet::next, disputedId);
    }

    public boolean existsOldFileId(Long fileId) {
        final String sql = "SELECT 1 FROM tr_transoutdspt d WHERE d.fileid = ?";
        return jdbcTemplate.query(sql, ResultSet::next, fileId.toString());
    }

    public List<TransOutDspt> queryOldDsptTransOutInfo(Long fileId) {
        final String sql =
                "SELECT d.*, e.transactionid, to_char(tr.msgid) AS messageid\n" +
                        "  FROM tr_transoutdspt d\n" +
                        "  JOIN tr_transout tr\n" +
                        "    ON tr.consumeid = d.consumeid\n" +
                        "  JOIN tr_transout_extend e\n" +
                        "    ON e.consumeid = tr.consumeid\n" +
                        " WHERE d.fileid = ?";
        return jdbcTemplate.query(sql, GenericResultSetExtractor.getInstance(TransOutDspt.class), fileId.toString());
    }

    public TransOutExtend queryTransOutExtendByConsumeId(String consumeId) {
        final String sql = "SELECT * FROM tr_transout_extend a WHERE a.consumeid = ?";
        List<TransOutExtend> transOutExtendList = jdbcTemplate.query(sql, GenericResultSetExtractor.getInstance(TransOutExtend.class), consumeId);
        return CollectionUtils.isEmpty(transOutExtendList) ? null : transOutExtendList.get(0);
    }

}
